import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:xinxuan/common/dialogs/transfer_dialog.dart';
import 'package:xinxuan/controllers/my_controller/blind_box_order_controller/blind_box_order_page_controller.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:xinxuan/vo/blindbox/receive_user_list_vo.dart';
import 'package:common_utils/common_utils.dart';

class ConversionPickingPageController extends GetxController {

  // BlindBoxOrderPageController? blindBoxOrderPageController;

  var orderId = 0.obs;
  var products = <Map<String, dynamic>>[].obs;
  var statusStr = ''.obs;
  var status = 0.obs;
  /// 是否列表页跳入
  var isOrderPage = true.obs;

  var mobile = ''.obs;
  var isSearch = false.obs;
  var receiveUserArr = <ReceiveUserItem>[].obs;

  @override
  void onInit() {
    print('onInit...');
    // blindBoxOrderPageController = Get.find();
    super.onInit();
  }
  
  @override
  void onReady() {
    Map<String, dynamic> argumentsMap = Get.arguments;
    orderId.value = argumentsMap['orderId'];
    products.value = argumentsMap['products'];
    statusStr.value = argumentsMap['statusStr'];
    status.value = argumentsMap['status'];
    isOrderPage.value = argumentsMap['isOrderPage'] ?? false;
    super.onReady();
  }


  /// 获取接受人列表
  getReceiveUserList() async {
    if (!RegexUtil.isMobileSimple(mobile.value)) {
      EasyLoading.showToast('请输入正确手机号');
      return;
    }
    ReceiveUserListVo? tempVo = await HttpUtilService.getReceiveUserList(mobile: mobile.value);
    if (tempVo == null) {
      receiveUserArr.value = [];
      update();
      return;
    }
    isSearch.value = true;
    if (tempVo.data!.length > 0) {
      receiveUserArr.value = tempVo.data!;
    } else {
      receiveUserArr.value = [];
    }
    update();
  }

  /// 确认赠送
  confirmGift(BlindBoxOrderPageController blindBoxOrderPageController) {
    Get.dialog(TransferDialog(nickname: '${receiveUserArr[0].username}', phone: '${receiveUserArr[0].mobile}', onTap: () async {
      var res = await HttpUtilService.confirmGift(receiver: '${receiveUserArr[0].mobile}', orderId: orderId.value);
      if (res) {
        blindBoxOrderPageController.getBlindBoxTabItemOrderList(blindBoxOrderPageController.tabIndex.value);
        if (isOrderPage.value) {
          Get.offAndToNamed(
            '${AppRouters.COMMON_PAGE}${AppRouters.BLINDBOX_ORDER_PAGE}${AppRouters.BLINDBOX_ORDER_DETAIL_PAGE}',
            arguments: {
              'id': orderId.value,
              'title': statusStr.value,
            }
          );
        } else {
          Get.back(result: true);
        }
      }
    }));
  }


}